package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /**
     * 查询当前登录用户的所有地址信息
     * @param userId
     * @return
     */
    @Select("select * from sky_take_out.address_book where user_id = #{userId}")
    List<AddressBook> list(Long userId);

    /**
     * 新增地址
     * @param addressBook
     */
    @Insert("insert into sky_take_out.address_book (user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label)" +
            "values (#{userId}, #{consignee}, #{sex}, #{phone}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, #{districtCode}, #{districtName}, #{detail}, #{label})")
    void add(AddressBook addressBook);



    /**
     * 返回该用户的默认地址对象
     * @return
     */
    @Select("select * from sky_take_out.address_book where user_id = #{userId} and is_default = 1")
    AddressBook queryIsDefault(Long currentId);

    /**
     * 将传进来的地址对象默认改为0
     * @param addressBook
     */
    @Update("update sky_take_out.address_book set is_default = 0 where user_id = #{userId} and is_default = 1 ")
    void uoDoDefault(AddressBook addressBook);

    /**
     * 将传进来主键找到地址对象默认设置为1
     * @param id
     */
    @Update("update sky_take_out.address_book set is_default = 1 where id = #{id}")
    void setDefault(Long id);

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Select("select * from sky_take_out.address_book where id = #{id}")
    AddressBook queryById(Long id);

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Update("update sky_take_out.address_book set " +
            "user_id = #{userId}," +
            "consignee = #{consignee}," +
            "sex = #{sex}," +
            "province_code = #{provinceCode}," +
            "province_name = #{provinceName}," +
            "city_code = #{cityCode}," +
            "city_name = #{cityName}," +
            "district_code = #{districtCode}," +
            "district_name = #{districtName}," +
            "detail = #{detail}," +
            "label = #{label} " +
            "where id = #{id}")
    void updateById(AddressBook addressBook);

    /**
     * 根据id删除地址
     * @param id
     */
    @Delete("delete from sky_take_out.address_book where id = #{id}")
    void deleteById(Long id);
}
